package keeto.com.vn.bms.ui.activity;

import java.util.ArrayList;

import javax.crypto.Mac;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.service.BMSReceiver;
import keeto.com.vn.bms.ui.adapter.HomeBMSTabAdpter;
import keeto.com.vn.bms.ui.fragment.BaseFragment;
import keeto.com.vn.bms.ui.fragment.CategoryBMSHomeFragment;
import keeto.com.vn.bms.ui.fragment.InfoBMSHomeFragment;
import keeto.com.vn.bms.ui.widget.LogoutDialog;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import keeto.com.vn.bms.utils.Utils;
import keeto.com.vn.bms.viewpagerindicator.TabPageIndicator;
import android.animation.AnimatorSet.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;


public class HomeBMSActivity extends FragmentActivity implements OnClickListener {
	public final static String ACTION_READ_NOTIFY = "Read_Notify";
	public final static String ACTION_READ_MESSAGER = "Read_Messager";

	private ImageButton btnLogin;
	public TabPageIndicator mTabPageIndicator;
	private ViewPager mViewPager;
	private HomeBMSTabAdpter mHomeBMSAdpter;
	private ArrayList<BaseFragment> list;
	private FragmentManager mMageger;
	private BMSSharedPreferences mPreferences;
	int type =0;
	private InfoBMSHomeFragment infoHome;
	private CategoryBMSHomeFragment categoryHome;
	private BMSReceiver bmsReceiver;
	private String mAction = null;
	private Bundle bundle  = null;
	private String ObjID;
	private HomeListener listener = new HomeListener();

	public TabPageIndicator getTabPageIndicator() {
		return mTabPageIndicator;
	}

	public static void actionLaunch(Context context) {
		Intent intent = new Intent(context, HomeBMSActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(intent);
	}
	public static void actionLaunchSpecial(Context context) {
		Intent intent = new Intent(context, HomeBMSActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_NO_ANIMATION);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.home_bms_layout);
		Intent intent = getIntent();


		if (intent != null){
			if (intent.getAction() != null){
				mAction = intent.getAction();
				bundle = intent.getExtras();
			}
		}
		mPreferences = new BMSSharedPreferences(HomeBMSActivity.this);
		type = mPreferences.getUserPermission(HomeBMSActivity.this);
		mMageger = getSupportFragmentManager();
		initUIComponents();
		registerReceiver();
		if (mAction != null && bundle != null){
			if (mAction.equals(ACTION_READ_NOTIFY)){
				ObjID = bundle.getString("NotifyId");
				BMSCore.getInstance(HomeBMSActivity.this).taskSetReadNotify(ObjID, mPreferences.getUserAuthKey(this), listener);

			}else if (mAction.equals(ACTION_READ_MESSAGER)){
				ObjID = bundle.getString("MsgId");
				BMSCore.getInstance(HomeBMSActivity.this).taskSetReadMessage(ObjID, mPreferences.getUserAuthKey(this), listener);
			}
		}

	}

	private void initUIComponents(){
		btnLogin = (ImageButton)findViewById(R.id.btn_home_login);
		btnLogin.setBackgroundResource(Utils.getLogobyPermission(type));
		mTabPageIndicator = (TabPageIndicator)findViewById(R.id.tab_indicator);
		mViewPager = (ViewPager)findViewById(R.id.home_pager);
		list = new ArrayList<BaseFragment>();
		if (type >0){
			categoryHome = CategoryBMSHomeFragment.newInstance();
			categoryHome.setTitle(this.getString(R.string.title_category));
			categoryHome.setDrawable(R.drawable.tab_category_home_selector);
			categoryHome.setNumbernotify("");

			infoHome = InfoBMSHomeFragment.newInstance();
			infoHome.setTitle(this.getString(R.string.title_info));
			infoHome.setDrawable(R.drawable.tab_info_home_selector);
			infoHome.setNumbernotify("");
			list.add(categoryHome);
			list.add(infoHome);
		}else{
			categoryHome = CategoryBMSHomeFragment.newInstance();
			categoryHome.setTitle(this.getString(R.string.title_category));
			categoryHome.setDrawable(R.drawable.tab_category_home_selector);
			categoryHome.setNumbernotify("");
			list.add(categoryHome);
		}
		mHomeBMSAdpter = new HomeBMSTabAdpter(mMageger, list);
		mViewPager.setAdapter(mHomeBMSAdpter);
		mTabPageIndicator.setViewPager(mViewPager);
		btnLogin.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void registerReceiver(){
		Intent intent = new Intent(HomeBMSActivity.this,BMSReceiver.class);
		intent.setAction(BMSReceiver.BMSRECEIVER_INTENT);
		this.sendBroadcast(intent);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_home_login: 
			if (mPreferences.getUserAuthKey(HomeBMSActivity.this).equals("")){
				LoginBMSActivity.actionLaunch(HomeBMSActivity.this);
			}else{
				LogoutDialog dialog = new LogoutDialog(HomeBMSActivity.this);
				dialog.show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (mViewPager != null && mViewPager.getCurrentItem() == 1){ 
			mViewPager.setCurrentItem(0);
		}else{
			super.onBackPressed();
		}

	}

	class HomeListener extends BMSListener{

	}


}
